import xml.etree.ElementTree as ET
from glob import glob
import pandas as pd
from tqdm import tqdm

annotations = sorted(glob('F:/items/bike-model/bike-train/origin-data/tiny/annotations/*.xml'))

df = []
cnt = 0
for file in tqdm(annotations):
    print(file)
    prev_filename = file.split('/')[-1].split('.')[0] + '.jpg'
    filename = str(cnt) + '.jpg'
    row = []
    parsedXML = ET.parse(file)
    for node in parsedXML.getroot().iter('object'):
        blood_cells = node.find('name').text
        xmin = int(node.find('bndbox/xmin').text)
        xmax = int(node.find('bndbox/xmax').text)
        ymin = int(node.find('bndbox/ymin').text)
        ymax = int(node.find('bndbox/ymax').text)

        row = [prev_filename, filename, blood_cells, xmin, xmax, ymin, ymax]
        df.append(row)
    cnt += 1

data = pd.DataFrame(df, columns=['prev_filename', 'filename', 'obj_name', 'xmin', 'xmax', 'ymin', 'ymax'])

data[['prev_filename', 'filename', 'obj_name', 'xmin', 'xmax', 'ymin', 'ymax']].to_csv(
    '../origin-data/tiny/tiny_detection.csv', index=False)

print(data.head(10))
